package com.shell.provider;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.po.PubUserExtra;
import com.shell.governor.user.po.PubUserOrgan;
import com.shell.governor.user.service.PubUserExtraService;
import com.shell.governor.user.service.PubUserService;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.user.vo.UserQueryVo;

import tk.mybatis.mapper.entity.Example;

@RestController
public class PubUserProvider {
	@Autowired
	private PubUserService pubUserService;
	@Autowired
	private PubUserExtraService pubUserExtraService;

	@RequestMapping({ "/user/selectByKey" })
	public PubUser selectByKey(@RequestParam("id") String id) {
		return (PubUser) this.pubUserService.selectByKey(id);
	}

	@RequestMapping({ "/user/saveUser" })
	int saveUser(@RequestBody PubUserVo user) {
		return this.pubUserService.saveUser(user);
	}

	@RequestMapping({ "/user/updateUser" })
	int updateUser(@RequestBody PubUserVo user) {
		return this.pubUserService.updateUser(user);
	}

	@RequestMapping({ "/user/updateAllUser" })
	void updateAllUser(@RequestBody PubUser user) {
		this.pubUserService.updateAll(user);
	}

	@RequestMapping({ "/user/saveOrUpdate" })
	void saveOrUpdate(@RequestBody PubUserVo pubUserVo) {
		PubUser user = new PubUser();
		BeanUtils.copyProperties(pubUserVo, user);
		this.pubUserService.saveOrUpdate(user);

		PubUserExtra userExtra = new PubUserExtra();
		userExtra.setUserId(pubUserVo.getUserId());
		userExtra.setEntId(pubUserVo.getEntId());
		PubUserExtra userExtra1 = (PubUserExtra) this.pubUserExtraService.selectOne(userExtra);
		userExtra.setUserState(pubUserVo.getUserState());
		if (userExtra1 != null) {
			this.pubUserExtraService.updateNotNull(userExtra);
		} else {
			this.pubUserExtraService.save(userExtra);
		}
	}

	@RequestMapping({ "/user/batchSaveUserOrgan" })
	void batchSaveUserOrgan(String userOrgans) throws Exception {
		List<PubUserOrgan> userOrganList = JsonUtil.fromJsonArray(userOrgans, PubUserOrgan.class);
		this.pubUserService.batchSaveUserOrgan(userOrganList);
	}

	@RequestMapping({ "/user/selectByEntity" })
	List<PubUserVo> selectByEntity(@RequestBody PubUser pubUser) {
		UserQueryVo userVo = new UserQueryVo();
		if (StringUtil.notTrimEmpty(pubUser.getUserId())) {
			userVo.setUserId(pubUser.getUserId());
		}
		if (StringUtil.notTrimEmpty(pubUser.getPhoneNo())) {
			userVo.setPhoneNo(pubUser.getPhoneNo());
		}
		if (StringUtil.notTrimEmpty(pubUser.getUserName())) {
			userVo.setUserName(pubUser.getUserName());
		}
		String[] notStates = { "D" };
		userVo.setNotStates(notStates);

		return this.pubUserService.queryUser(userVo);
	}

	@RequestMapping({ "/user/selectByCreatorId" })
	List<PubUser> selectByCreatorId(@RequestParam("creatorId") String creatorId) {
		Example example = new Example(PubUser.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("creatorId", creatorId);
		return this.pubUserService.selectByExample(example);
	}

	@RequestMapping({ "/user/getUserByRoleId" })
	public List<PubUser> getUserByRoleId(@RequestParam("RoleId") String roleId) {
		return this.pubUserService.selectUserByRoleId(roleId);
	}

	@RequestMapping({ "/user/selectAllUserForEnt" })
	List<PubUserVo> selectAllUserForEnt(@RequestParam("entId") String entId) {
		return this.pubUserService.selectAllUserForEnt(entId);
	}

	@RequestMapping({ "/user/selectUserByRoleCodeAndEntId" })
	List<PubUserVo> selectUserByRoleCodeAndEntId(@RequestParam("entId") String entId,
			@RequestParam("roleCode") String roleCode) {
		return this.pubUserService.selectUserByRoleCodeAndEntId(entId, roleCode);
	}
}
